<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2018/1/26
 * Time: 21:59
 */

namespace app\common\helper;



use think\facade\Request;

class ResponseHelper
{
    const COMMON_ERROR_MESSAGE  = '操作失败,请稍后重试';
    const COMMON_SUCCESS_MESSAGE  = '操作成功';

    public static function _failure($msg){
        if(Request::isPost() || Request::isAjax()){
            $data = array('status'=>0,'msg'=>$msg,'info'=>$msg);
            // 返回JSON数据格式到客户端
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode($data));
        }else{
            self::_alert($msg);
        }
    }


    public static function _success($msg,$url=''){
        $data = array('status'=>1,'msg'=>$msg,'url'=>$url,'info'=>$msg);
        // 返回JSON数据格式到客户端
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($data));
    }

    public static function _alert($msg,$url=''){
        if($url == ''){
            $js = "window.history.back();";
        }else{
            $js = "window.location.href='".$url."'";
        }
        echo '<!DOCTYPE html>
<html>
<head lang="zh-cn">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta content="telephone=no" name="format-detection">';
        echo '<body>';
        echo '<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>';
        echo '<script type="text/javascript" src="/static/lib/layer/layer.js"></script>';
        echo '<script type="text/javascript">';
        echo "layer.open({content: '".$msg."',btn: ['确认'],shadeClose: false,yes: function(){{$js}}});";
        echo '</script></body></html>';
        exit;
    }

    public static function jsonEncode($arr){
        return json_encode($arr, JSON_UNESCAPED_UNICODE);
    }
}